package com.mrd.shiptask_shipowner.unit_tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.mrd.shiptask_shipowner.shiareutil.SharePreferenceUtil;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("SimpleDateFormat")
public class FunctionOfActivity {
	public static final String DEFAULT_FORMAT_TYPE = "yyyy-MM-dd";

	/**
	 * 检测当的网络（WLAN、3G/2G）状态
	 * 
	 * @param context
	 *            Context
	 * @return true 表示网络可用
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info != null && info.isConnected()) {
				// 当前网络是连接的
				if (info.getState() == NetworkInfo.State.CONNECTED) {
					// 当前所连接的网络可用
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * toask提示
	 */
	public static void makeToaskShow(Context context, String showText) {
		try {
			Toast.makeText(context, showText, Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	/**
	 * 获取设备IMEI，作为设备唯一标识码
	 * 
	 * @param ctx
	 * @return
	 */
	public static String getDeviceId(Context ctx) {
		SharePreferenceUtil sp = new SharePreferenceUtil(ctx);
		if ("".equals(sp.getUUID())) {
			TelephonyManager tm = (TelephonyManager) ctx
					.getSystemService(Context.TELEPHONY_SERVICE);
			sp.setUUID(tm.getDeviceId());
		}
		return sp.getUUID();
	}

	/**
	 * 获取当前点击位置是否为et
	 * 
	 * @param view
	 *            焦点所在View
	 * @param event
	 *            触摸事件
	 * @return
	 */
	public static boolean isClickEt(View view, MotionEvent event) {
		if (view != null && (view instanceof EditText)) {
			int[] leftTop = { 0, 0 };
			// 获取输入框当前的location位置
			view.getLocationInWindow(leftTop);
			int left = leftTop[0];
			int top = leftTop[1];
			// 此处根据输入框左上位置和宽高获得右下位置
			int bottom = top + view.getHeight();
			int right = left + view.getWidth();
			if (event.getX() > left && event.getX() < right
					&& event.getY() > top && event.getY() < bottom) {
				// 点击的是输入框区域，保留点击EditText的事件
				return false;
			} else {
				return true;
			}
		}
		return false;
	}

	/**
	 * 以指定格式格式化时间
	 * 
	 * @param dateStr
	 *            需要格式化的时间
	 * @param formatType
	 *            格式
	 * @return 格式化后的值
	 * @throws ParseException
	 */
	@SuppressLint("SimpleDateFormat")
	public static Date parseString(String dateStr, String formatType) {
		try {
			if (formatType == null || "".equals(formatType)) {
				formatType = DEFAULT_FORMAT_TYPE;
			}
			SimpleDateFormat sdf = new SimpleDateFormat(formatType);
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String addZero(String str) {
		if (str.length() < 2) {
			str = "0" + str;
		}
		return str;

	}

	/**
	 * 获取年份
	 * 
	 * @return
	 */
	public static String getYearFromDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date dateInfo = new Date();
		return sdf.format(dateInfo);

	}

	/**
	 * 获取月份
	 * 
	 * @return
	 */
	public static String getMonthFromDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		Date dateInfo = new Date();
		return sdf.format(dateInfo);

	}

	/**
	 * 获取日
	 * 
	 * @return
	 */
	public static String getDayFromDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		Date dateInfo = new Date();
		return sdf.format(dateInfo);

	}

	/**
	 * 获取日(当天+1)
	 * 
	 * @return
	 */
	public static String getDayFromDateAddOneDay() {
		String str = "";
		try {
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-mm-dd");
			Date beginDate = new Date();
			Calendar date = Calendar.getInstance();
			date.setTime(beginDate);
			date.set(Calendar.DATE, date.get(Calendar.DATE) + 1);
			Date endDate;
			endDate = dft.parse(dft.format(date.getTime()));
			SimpleDateFormat sdf = new SimpleDateFormat("dd");
			str = sdf.format(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			str = "00";
		}
		return str;

	}

	/**
	 * 自定义progressDialog，不可手动取消
	 */
	public static void progressDialogShow(Context context) {
		final ProgressDialog proDia = ProgressDialog.show(context, "Loading",
				"正在跳转，请稍候...", true, false);
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					proDia.dismiss();// 关闭ProgressDialog
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		t.start();
	}

	/**
	 * 判断选择的时候是否今天之后
	 * 
	 * @param context
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static boolean checkDataIsAfterToday(Context context, int year,
			String month, String day) {
		// TODO Auto-generated method stub
		try {
			// 将年月日拼成8位数数字，对比大小来判断选择日期是否今天之后
			// 由于服务器规定延期两天才可成功
			String systemData = getYearFromDate() + getMonthFromDate()
					+ getDayFromDateAddOneDay();
			String infoData = year + month + day;
			int systemD = Integer.parseInt(systemData);
			int infoD = Integer.parseInt(infoData);
			if (infoD <= systemD) {
				return true;
			}
			return false;
		} catch (Exception e) {
			return true;
		}
	}
}
